import { Component, Input, ChangeDetectionStrategy, ChangeDetectorRef } from "@angular/core";
import * as _ from 'lodash';
import { GroupedIncidentsModel } from "../../controversies/components/controversiesSummary/models/groupedIncidents.model";
import { IncidentCategory } from "../../../../../shared/models/incidentCategory.enum";
import { environment } from "../../../../../../environments/environment";
import { ControversyQualitativePerformanceModel, ControversyEventIndicatorModel } from "../../../../../shared/models/qualitativePerformance.model";
import { TouchSequence } from "selenium-webdriver";

/**
 * Controversies summary component
 *
 * Author: Sorin Spiru
 * Date: 22.03.2018
 */
@Component({
    selector: 'controversy-summary',
    templateUrl: 'companyControversiesSummary.component.html',
    styleUrls: [
        '../../company.component.scss',
        '../../controversies/companyControversies.component.scss',
        'companyControversiesSummary.component.scss'
    ],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class CompanyControversiesSummaryComponent {
    public maxAssessment: number;
    public highestIncidentOutlookAssessment: string;
    public highestIncidents: ControversyQualitativePerformanceModel[];
    public incidentsByCategory: GroupedIncidentsModel[];
    public incidentCategory: typeof IncidentCategory = IncidentCategory;
    public readonly resourcesUrl: string = environment.resourcesUrl;
    private eventIndicators: ControversyEventIndicatorModel[] = [];

    @Input() companyId: any;
    @Input() set incidents(value: ControversyQualitativePerformanceModel[]) {
        if (value) {
            this.maxAssessment = Math.max.apply(Math, value.map(x => { return x.answerCategory; }));
            //get incidents with the highest assessment
            var highestIncidentOutlookScore = 0;
            this.highestIncidents = value.filter(x => x.answerCategory == this.maxAssessment);
            this.highestIncidents.forEach(controversyindicator =>{
                var eventIndicators =  controversyindicator.eventIndicators.filter(ev=> ev.answerCategory == this.maxAssessment);
                eventIndicators.forEach(eventIndicator =>{
                    if(eventIndicator.outlookScore != null && eventIndicator.outlookScore != highestIncidentOutlookScore)
                    {
                        if(eventIndicator.outlookScore == -1)
                            {highestIncidentOutlookScore = eventIndicator.outlookScore;}
                            else if (eventIndicator.outlookScore == 1 && highestIncidentOutlookScore != -1)
                            {highestIncidentOutlookScore = eventIndicator.outlookScore;}
                    }
                })
                
            })
            switch (highestIncidentOutlookScore)
            {
                case -1:
                    this.highestIncidentOutlookAssessment =  "Negative";break;
                case 0:
                    this.highestIncidentOutlookAssessment =  "Neutral";break;
                case 1:
                    this.highestIncidentOutlookAssessment =  "Positive";break;
                default:
                    this.highestIncidentOutlookAssessment = "";break;
            }
            
            value.forEach(incident => {
                incident.eventIndicators.forEach(indicator => {
                    indicator.controversyIndicatorNumber = incident.number;
                });
                this.eventIndicators = this.eventIndicators.concat(incident.eventIndicators);
            });
            
            //get count of incident for each assessment level
            this.incidentsByCategory = _(this.eventIndicators).groupBy('answerCategory').map(function (items, key) {
                return new GroupedIncidentsModel(key, items)
            }).value();
            this.cdRef.markForCheck();
        }
    }

    constructor(private cdRef: ChangeDetectorRef) { }
}